package cn.ios.hrm.service.impl;

import cn.ios.hrm.constant.EmployeeTypeConstant;
import cn.ios.hrm.domain.Employee;
import cn.ios.hrm.domain.LoginUser;
import cn.ios.hrm.domain.Tenant;
import cn.ios.hrm.dto.RegistDto;
import cn.ios.hrm.feign.ILoginUserFeignClient;
import cn.ios.hrm.mapper.EmployeeMapper;
import cn.ios.hrm.mapper.TenantMapper;
import cn.ios.hrm.query.TenantQuery;
import cn.ios.hrm.service.ITenantService;
import cn.ios.hrm.util.AjaxResult;
import cn.ios.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zy
 * @since 2021-12-16
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private ILoginUserFeignClient loginUserFeignClient;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Page<Tenant> loadTenantList(TenantQuery query, Page<Tenant> page) {
        List<Tenant> tlist = tenantMapper.loadTenantList(query,page);
        page.setRecords(tlist);
        return page;
    }

    @Override
    public AjaxResult entering(RegistDto dto) {
        //先获取employee
        Employee employee = dto.getEmployee();
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(employee.getUsername());
        loginUser.setPassword(employee.getPassword());
        loginUser.setMealId(dto.getMealId());
        //保存auth服务
        AjaxResult result = loginUserFeignClient.TenantEntering(loginUser);
        if (result.isSuccess()){
            //保存t_employee
            Integer loginId = (Integer)result.getResultObj();
            employee.setLoginId(Long.valueOf(loginId));
            employee.setType(EmployeeTypeConstant.ADMIN_EMP);
            Date date = new Date();
            employee.setInputTime(date);
            employeeMapper.insert(employee);
            //保存t_tenant
            Tenant tenant = dto.getTenant();
            tenant.setAdminId(employee.getId());
            tenant.setRegisterTime(date);
            tenantMapper.insert(tenant);
        }
        return result;
    }
}
